package com.example.goods_manage.service.impl;

import com.example.goods_manage.dto.User;
import com.example.goods_manage.mapper.UserMapper;
import com.example.goods_manage.service.UserService;
import com.example.goods_manage.utils.AjaxResult;
import com.example.goods_manage.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/7/18:10:14
 */

@SuppressWarnings({"unchecked", ""})
@Service
public class UserServiceImpl implements UserService {
    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Override
    public AjaxResult login(User user) {
        User account = userMapper.findUserByAccount(user);

        if(ObjectUtils.isEmpty(account)){
            return AjaxResult.error("用户不存在");
        }

        if(!account.getPassword().equals(user.getPassword())){
            return AjaxResult.error("密码不正确");
        }
        String token = JWTUtil.generateToken(account.getId());
        account.setPassword(null);
        account.setToken(token);
        return AjaxResult.success(account);
    }
}
